package com.yhhvip.lottery.controller;

import com.github.pagehelper.PageInfo;
import com.yhhvip.lottery.dao.mybatis.model.LotAdv;
import com.yhhvip.lottery.dao.mybatis.model.LotUser;
import com.yhhvip.lottery.domain.*;
import com.yhhvip.lottery.domain.res.ResUserLotteryVo;
import com.yhhvip.lottery.service.LotAdvService;
import com.yhhvip.lottery.service.LotUserService;
import com.yhhvip.utils.BeanValidators;
import com.yhhvip.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author XOu
 * @version 1.0
 * @description 广告控制层
 * @date 2018/6/19
 * @modified
 */
@Slf4j
@RestController
@Api(description = "广告相关API")
@RequestMapping("adv")
public class LotAdvController {

    @Autowired
    private LotAdvService lotAdvService;


    @ApiOperation(value = "添加广告位", produces = "application/json", notes = "添加广告位")
    @ApiImplicitParam(name = "reqLotAdvVo", value = "请求参数实体", required = true, dataType = "ReqLotAdvVo")
    @PostMapping
    public JsonResult add(@RequestBody ReqLotAdvVo reqLotAdvVo){
        BeanValidators.validate(reqLotAdvVo);
        Long advId = lotAdvService.add(reqLotAdvVo);
        if (null != advId) {
            return JsonResult.ok(advId);
        }
        return JsonResult.build("添加失败");
    }

    @ApiOperation(value = "编辑广告位", produces = "application/json", notes = "编辑广告位")
    @ApiImplicitParam(name = "reqLotAdvVo", value = "请求参数实体", required = true, dataType = "ReqLotAdvVo")
    @PutMapping
    public JsonResult update(@RequestBody ReqLotAdvVo reqLotAdvVo){
        BeanValidators.validate(reqLotAdvVo);
        Long id = reqLotAdvVo.getId();
        if (null == id) {
            return JsonResult.build("主键ID不能为空");
        }
        int i = lotAdvService.update(reqLotAdvVo);
        if (1 == i) {
            return JsonResult.ok();
        }
        return JsonResult.build("编辑失败");
    }

    @ApiOperation(value = "删除广告位", produces = "application/json", notes = "删除广告位")
    @ApiImplicitParam(paramType = "path", name = "id", value = "广告ID", required = true, dataType = "Long", example = "1")
    @DeleteMapping("{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        int i = lotAdvService.delete(id);
        if (1 == i) {
            return JsonResult.ok();
        }
        return JsonResult.build("删除失败");
    }

}
